﻿namespace KeRui.BLL
{
    using KeRui.Common;
    using KeRui.DAL;
    using KeRui.Model;
    using System;
    using System.Collections.Generic;
    using System.Data;

    public class CmsNode
    {
        private readonly KeRui.DAL.CmsNode dal = new KeRui.DAL.CmsNode();

        public int Add(KeRui.Model.CmsNode model)
        {
            return this.dal.Add(model);
        }

        public List<KeRui.Model.CmsNode> DataTableToList(DataTable dt)
        {
            List<KeRui.Model.CmsNode> list = new List<KeRui.Model.CmsNode>();
            int count = dt.Rows.Count;
            if (count > 0)
            {
                for (int i = 0; i < count; i++)
                {
                    KeRui.Model.CmsNode item = new KeRui.Model.CmsNode();
                    if (dt.Rows[i]["NodeID"].ToString() != "")
                    {
                        item.NodeID = int.Parse(dt.Rows[i]["NodeID"].ToString());
                    }
                    item.NodeName = dt.Rows[i]["NodeName"].ToString();
                    item.NodeKey = dt.Rows[i]["NodeKey"].ToString();
                    item.NodePath = dt.Rows[i]["NodePath"].ToString();
                    item.NodeDir = dt.Rows[i]["NodeDir"].ToString();
                    if (dt.Rows[i]["SiteID"].ToString() != "")
                    {
                        item.SiteID = int.Parse(dt.Rows[i]["SiteID"].ToString());
                    }
                    if (dt.Rows[0]["ContentPageSize"].ToString() != "")
                    {
                        item.ContentPageSize = int.Parse(dt.Rows[0]["ContentPageSize"].ToString());
                    }
                    if (dt.Rows[i]["ModelID"].ToString() != "")
                    {
                        item.ModelID = int.Parse(dt.Rows[i]["ModelID"].ToString());
                    }
                    if (dt.Rows[i]["ServerID"].ToString() != "")
                    {
                        item.ServerID = int.Parse(dt.Rows[i]["ServerID"].ToString());
                    }
                    if (dt.Rows[i]["ParentID"].ToString() != "")
                    {
                        item.ParentID = int.Parse(dt.Rows[i]["ParentID"].ToString());
                    }
                    item.ParentPath = dt.Rows[i]["ParentPath"].ToString();
                    if (dt.Rows[i]["Depth"].ToString() != "")
                    {
                        item.Depth = int.Parse(dt.Rows[i]["Depth"].ToString());
                    }
                    if (dt.Rows[i]["RootID"].ToString() != "")
                    {
                        item.RootID = int.Parse(dt.Rows[i]["RootID"].ToString());
                    }
                    if (dt.Rows[i]["ChildNum"].ToString() != "")
                    {
                        item.ChildNum = int.Parse(dt.Rows[i]["ChildNum"].ToString());
                    }
                    item.arrChildID = dt.Rows[i]["arrChildID"].ToString();
                    if (dt.Rows[i]["PrevID"].ToString() != "")
                    {
                        item.PrevID = int.Parse(dt.Rows[i]["PrevID"].ToString());
                    }
                    if (dt.Rows[i]["NextID"].ToString() != "")
                    {
                        item.NextID = int.Parse(dt.Rows[i]["NextID"].ToString());
                    }
                    if (dt.Rows[i]["OrderID"].ToString() != "")
                    {
                        item.OrderID = int.Parse(dt.Rows[i]["OrderID"].ToString());
                    }
                    item.Tips = dt.Rows[i]["Tips"].ToString();
                    item.Description = dt.Rows[i]["Description"].ToString();
                    item.TemplateFile = dt.Rows[i]["TemplateFile"].ToString();
                    if (dt.Rows[i]["NodeType"].ToString() != "")
                    {
                        item.NodeType = int.Parse(dt.Rows[i]["NodeType"].ToString());
                    }
                    item.NodePicUrl = dt.Rows[i]["NodePicUrl"].ToString();
                    item.NodeLinkUrl = dt.Rows[i]["NodeLinkUrl"].ToString();
                    item.MetaKeywords = dt.Rows[i]["MetaKeywords"].ToString();
                    item.MetaDescription = dt.Rows[i]["MetaDescription"].ToString();
                    if (dt.Rows[i]["ShowOnMenu"].ToString() != "")
                    {
                        if ((dt.Rows[i]["ShowOnMenu"].ToString() == "1") || (dt.Rows[i]["ShowOnMenu"].ToString().ToLower() == "true"))
                        {
                            item.ShowOnMenu = true;
                        }
                        else
                        {
                            item.ShowOnMenu = false;
                        }
                    }
                    if (dt.Rows[i]["CommentCount"].ToString() != "")
                    {
                        item.CommentCount = int.Parse(dt.Rows[i]["CommentCount"].ToString());
                    }
                    if (dt.Rows[i]["ContentCount"].ToString() != "")
                    {
                        item.ContentCount = int.Parse(dt.Rows[i]["ContentCount"].ToString());
                    }
                    item.ContentDefaultTemplateFile = dt.Rows[i]["ContentDefaultTemplateFile"].ToString();
                    item.CommentTemplate = dt.Rows[i]["CommentTemplate"].ToString();
                    item.CreateUser = dt.Rows[i]["CreateUser"].ToString();
                    if (dt.Rows[i]["CreateTime"].ToString() != "")
                    {
                        item.CreateTime = DateTime.Parse(dt.Rows[i]["CreateTime"].ToString());
                    }
                    if (dt.Rows[i]["UpdateTime"].ToString() != "")
                    {
                        item.UpdateTime = DateTime.Parse(dt.Rows[i]["UpdateTime"].ToString());
                    }
                    list.Add(item);
                }
            }
            return list;
        }

        public int Delete(int NodeID)
        {
            return this.dal.Delete(NodeID);
        }

        public List<KeRui.Model.CmsNode> GetList(int SiteID)
        {
            string strWhere = "SiteID=" + SiteID;
            string strOrder = "nodeid asc";
            int pageSize = -1;
            int pageIndex = -1;
            DataSet set = new KeRui.DAL.Common().GetList("CmsNode", pageSize, pageIndex, strWhere, strOrder);
            return this.DataTableToList(set.Tables[0]);
        }

        public List<KeRui.Model.CmsNode> GetList(string strWhere)
        {
            string strOrder = "nodeid asc";
            int pageSize = -1;
            int pageIndex = -1;
            DataSet set = new KeRui.DAL.Common().GetList("CmsNode", pageSize, pageIndex, strWhere, strOrder);
            return this.DataTableToList(set.Tables[0]);
        }

        public List<KeRui.Model.CmsNode> GetListByCache(int SiteID)
        {
            string cacheKey = "CmsNodeList-" + SiteID;
            object cache = DataCache.GetCache(cacheKey);
            if (cache == null)
            {
                try
                {
                    cache = this.GetList(SiteID);
                    if (cache != null)
                    {
                        int configInt = ConfigHelper.GetConfigInt("DataCache");
                        DataCache.SetCache(cacheKey, cache, DateTime.Now.AddMinutes((double) configInt), TimeSpan.Zero);
                    }
                }
                catch
                {
                }
            }
            return (List<KeRui.Model.CmsNode>) cache;
        }

        public List<KeRui.Model.CmsNode> GetListBynodeType(int SiteID, int nodeType)
        {
            string strWhere = string.Concat(new object[] { " SiteID = ", SiteID, " AND nodeType=", nodeType });
            string strOrder = " orderid asc";
            int pageSize = -1;
            int pageIndex = -1;
            DataSet set = new KeRui.DAL.Common().GetList("CmsNode", pageSize, pageIndex, strWhere, strOrder);
            return this.DataTableToList(set.Tables[0]);
        }

        public List<KeRui.Model.CmsNode> GetListByPId(int pNoteID)
        {
            string strWhere = "ParentID=" + pNoteID;
            string strOrder = "nodeid asc";
            int pageSize = -1;
            int pageIndex = -1;
            DataSet set = new KeRui.DAL.Common().GetList("CmsNode", pageSize, pageIndex, strWhere, strOrder);
            return this.DataTableToList(set.Tables[0]);
        }

        public List<KeRui.Model.CmsNode> GetListByPId(int pNoteID, int SiteID)
        {
            string strWhere = string.Concat(new object[] { " SiteID = ", SiteID, " AND ParentID=", pNoteID });
            string strOrder = " orderid asc";
            int pageSize = -1;
            int pageIndex = -1;
            DataSet set = new KeRui.DAL.Common().GetList("CmsNode", pageSize, pageIndex, strWhere, strOrder);
            return this.DataTableToList(set.Tables[0]);
        }

        public List<KeRui.Model.CmsNode> GetListByPId(int pNoteID, int SiteID, int Temp)
        {
            string strWhere = string.Concat(new object[] { " SiteID = ", SiteID, " AND ParentID=", pNoteID });
            if (Temp != 0)
            {
                object obj2 = strWhere;
                strWhere = string.Concat(new object[] { obj2, " AND ParentID <> ", Temp, " and NodeID <>", Temp });
            }
            string strOrder = "nodeid asc";
            int pageSize = -1;
            int pageIndex = -1;
            DataSet set = new KeRui.DAL.Common().GetList("CmsNode", pageSize, pageIndex, strWhere, strOrder);
            return this.DataTableToList(set.Tables[0]);
        }

        public List<KeRui.Model.CmsNode> GetListByPIdandServerID(int pNoteID, int ServerID)
        {
            string strWhere = string.Concat(new object[] { " ServerID = ", ServerID, " AND ParentID=", pNoteID });
            string strOrder = "nodeid asc";
            int pageSize = -1;
            int pageIndex = -1;
            DataSet set = new KeRui.DAL.Common().GetList("CmsNode", pageSize, pageIndex, strWhere, strOrder);
            return this.DataTableToList(set.Tables[0]);
        }

        public KeRui.Model.CmsNode GetModel(int NodeID)
        {
            return this.dal.GetModel(NodeID);
        }

        public KeRui.Model.CmsNode GetModel(int SiteID, string NodePath)
        {
            return this.dal.GetModel(SiteID, NodePath);
        }

        public KeRui.Model.CmsNode GetModelByCache(int NodeID)
        {
            string cacheKey = "CmsNodeModel-" + NodeID;
            object cache = DataCache.GetCache(cacheKey);
            if (cache == null)
            {
                try
                {
                    cache = this.dal.GetModel(NodeID);
                    if (cache != null)
                    {
                        int configInt = ConfigHelper.GetConfigInt("DataCache");
                        DataCache.SetCache(cacheKey, cache, DateTime.Now.AddMinutes((double) configInt), TimeSpan.Zero);
                    }
                }
                catch
                {
                }
            }
            return (KeRui.Model.CmsNode) cache;
        }

        public KeRui.Model.CmsNode GetModelByCache(int SiteID, string NodePath)
        {
            string cacheKey = string.Concat(new object[] { "CmsNodeModel-", SiteID, "-", NodePath });
            object cache = DataCache.GetCache(cacheKey);
            if (cache == null)
            {
                try
                {
                    cache = this.dal.GetModel(SiteID, NodePath);
                    if (cache != null)
                    {
                        int configInt = ConfigHelper.GetConfigInt("DataCache");
                        DataCache.SetCache(cacheKey, cache, DateTime.Now.AddMinutes((double) configInt), TimeSpan.Zero);
                    }
                }
                catch
                {
                }
            }
            return (KeRui.Model.CmsNode) cache;
        }

        public DataSet GetSelect(int ServerID)
        {
            string strWhere = " ServerID = " + ServerID;
            string strOrder = " orderid asc";
            KeRui.DAL.Common common = new KeRui.DAL.Common();
            return common.GetSelect("CmsNode", strWhere, strOrder, " NodeID, NodeName");
        }

        public int sort(int NodeID, int Flag)
        {
            return this.dal.sort(NodeID, Flag);
        }

        public int Update(KeRui.Model.CmsNode model)
        {
            return this.dal.Update(model);
        }

        public void UpdateNodeInfo(int nodeid, int currentID)
        {
            this.dal.UpdateNodeInfo(nodeid, currentID);
        }
    }
}

